package cn.david.pay.service.impl;



import cn.david.basic.exception.BusinessExcepiton;
import cn.david.basic.service.impl.BasicServiceImpl;
import cn.david.order.service.IProductOrderService;
import cn.david.pay.domain.AlipayInfo;
import cn.david.pay.domain.PayBill;
import cn.david.pay.mapper.AlipayInfoMapper;
import cn.david.pay.mapper.PayBillMapper;
import cn.david.pay.service.IPayBillService;
import cn.david.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BasicServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill bill) {
        if (bill==null){
            throw new BusinessExcepiton("请创建支付订单在进行支付！！");
        }
        PayBill billTmp = payBillMapper.loadByUnionPaySn(bill.getUnionPaySn());
        if (billTmp==null){
            throw new BusinessExcepiton("请创建支付订单在进行支付！！");
        }
        switch (bill.getPayChannel().intValue()){
            case 1:
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AlipayUtils.pay(info, bill);
            case 2:
                break;
            case 3:
                break;
            default:

        }
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }

}
